package com.remoter.basic.util;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author			koko
 * @date 			2017-08-10 11:17:08
 * @description 	类功能说明
 */
public class ConcurrentTwoKeyOneValueHashMap<KA,KB,V> {
	
	private ConcurrentHashMap<KA,ConcurrentHashMap<KB,V>> maps = new ConcurrentHashMap<KA,ConcurrentHashMap<KB,V>>();
	
	public ConcurrentTwoKeyOneValueHashMap(){
		this.maps.clear();
	}
	
	public ConcurrentTwoKeyOneValueHashMap<KA,KB,V> set(KA ka,KB kb,V v){
		if(null == ka || null == kb || null == v){
			throw new IllegalArgumentException("key or value is not null");
		}
		ConcurrentHashMap<KB,V> child = maps.get(ka);
		if(null == child){
			maps.put(ka,new ConcurrentHashMap<KB,V>());
			child = maps.get(ka);
		}
		child.put(kb,v);
		return this;
	}
	
	public ConcurrentTwoKeyOneValueHashMap<KA,KB,V> del(KA ka,KB kb){
		if(null == ka || null == kb){
			throw new IllegalArgumentException("key is not null");
		}
		if(this.maps.containsKey(ka) && this.maps.get(ka).containsKey(kb)){
			this.maps.get(ka).remove(kb);
		}
		return this;
	}
	
	public ConcurrentTwoKeyOneValueHashMap<KA,KB,V> del(KA ka){
		if(this.maps.containsKey(ka)){
			this.maps.remove(ka);
		}
		return this;
	}
	
	public V getValue(KA ka,KB kb){
		if(maps.containsKey(ka) && maps.get(ka).containsKey(kb)){
			return maps.get(ka).get(kb);
		}else{
			return null;
		}
	}
	
	public boolean isEmpty(){
		return this.maps.isEmpty();
	}
	
	public boolean isKeyAEmpty(KA ka){
		if(this.maps.containsKey(ka)){
			return this.maps.get(ka).isEmpty();
		}else{
			return true;
		}
	}
	
	public boolean containsKey(KA ka){
		return this.maps.containsKey(ka);
	}
	
	public boolean containsKey(KA ka,KB kb){
		if(this.containsKey(ka)){
			return this.maps.get(ka).containsKey(kb);
		}else{
			return false;
		}
	}
	
	public ConcurrentTwoKeyOneValueHashMap<KA,KB,V> clear(){
		this.maps.clear();
		return this;
	}
	
	public Set<Map.Entry<KA,ConcurrentHashMap<KB,V>>> entrySet(){
		return this.maps.entrySet();
	}
	
	@Override
	public String toString() {
		return this.maps.toString();
	}
	
}